library(tidyverse)
library(magrittr)
library(lubridate)

# replicates figure 1w

c(
  "https://github.com/thomasjwood/cgptw22/raw/main/all_panels_long.rds",
  "https://github.com/thomasjwood/cgptw22/raw/main/treatments_df.rds"
  ) %>% 
  map2(
    str_c("t", 0:1),
    \(i, j)
    i %>% 
      url %>% 
      gzcon %>% 
      readRDS %>% 
      assign(
        x = j, 
        envir = .GlobalEnv
      )
  )

t2 <- t0 %>% 
  group_by(
    congeniality, lucid_pid_3, treatment, topic_short_2
  ) %>% 
  summarize(
    mu = outcome_w1 %>% mean(na.rm = T),
    se = outcome_w1 %>% plotrix::std.error(na.rm = T)
  ) %>% 
  mutate(
    lo = mu %>% 
      subtract(
        se %>% multiply_by(1.96)
      ),
    hi = mu %>% 
      add(
        se %>% multiply_by(1.96)
      )
  ) %>% 
  filter(
    treatment %>% 
      equals("control") &
    lucid_pid_3 %>% 
      is_in(
        c("Democrat",
          "Republican")
        )
    ) %>% 
  mutate(
    cfac = "Agreement in control"
  ) %>% 
  bind_rows(
    t1 %>% 
      select(
        topic_short_2, congeniality, traffic
      ) %>% 
      mutate(
        cfac = "Fact check website traffic"
      )  
  ) %>% 
  left_join(
    t1 %>% 
      select(
        topic_short_2, date_fielded
      ) %>% 
      mutate(
        ts2 = topic_short_2 %>% 
          str_c(
            " (",
            date_fielded %>% 
              mday,
            " ",
            date_fielded %>% 
              month(label = T, abbr = T),
            ")"
          )
      ) %>% 
      select(-date_fielded)
  ) %>% 
  mutate(
    cfac = cfac %>% 
      factor(
        c("Fact check website traffic",
          "Agreement in control")
      ) ,
    congeniality = congeniality %>% 
      str_wrap(
        width = 15
      ) %>% 
      factor(
        c("False claim\ncongenial to\nRepublicans",
          "False claim\ncongenial to\nDemocrats")
      )
    )

t2$ts2 %<>%
  factor(
    t2 %>% 
      filter(
        traffic %>% 
          is.na %>% 
          not
        ) %>% 
      group_by(ts2) %>% 
      summarize(traf = traffic %>% max) %>% 
      arrange(traf) %>% 
      use_series(ts2)
    )

t2$cfac %<>% 
  fct_rev

ggplot() +
  geom_col(
    aes(
      traffic, ts2  
    ),
    data = t2 %>% 
      filter(
        cfac %>% 
          str_detect("traffic")
      ),
    fill = "grey98",
    color = "grey2",
    size = .2,
    width = .55
  ) +
  geom_linerange(
    aes(
      xmin = lo,
      xmax = hi,
      y = ts2,
      group = lucid_pid_3
    ),
    data = t2 %>% 
      filter(
        cfac %>% 
          str_detect("traffic", negate = T)
        
      ),
    position = position_dodge(width = .1),
    size = .3
  ) +
  geom_point(
    aes(
      mu, ts2,  fill = lucid_pid_3 
    ),
    shape = 21,
    size = 2.5,
    data = t2 %>% 
      filter(
        cfac %>% 
          str_detect("traffic", negate = T)
        ),
    position = position_dodge(width = .1)
  ) +
  geom_text(
    aes(
      x, y, label = lab
    ),
    hjust = 0,
    size = 2.7,
    lineheight = .7,
    fontface = "italic",
    data = tribble(
      ~x, ~y, ~lab,
      45, t2$ts2 %>% levels %>% extract2(24),  "Republicans",
      20.5, t2$ts2 %>% levels %>% extract2(24),  "Democrats"
      ) %>%
      mutate(
        y = y %>%
          factor(t2$ts2 %>% levels),
        cfac = t2$cfac %>%
          levels %>%
          extract2(1) %>%
          factor(
            t2$cfac %>%
              levels
            ),
        congeniality = t2$congeniality %>%
          levels %>%
          extract2(1) %>%
          factor(
            t2$congeniality %>%
              levels
          )
      )
  ) +
  facet_grid(
    congeniality ~ cfac,
    scales = "free",
    space = "free_y", 
    drop = T
  ) 